﻿namespace Ficharts.Net
{
    using System.ComponentModel;
    using System.Data;

    using Ficharts.Net.Enums;

    using Newtonsoft.Json;

    public abstract class SingleSeriesChart : FiChartBase
    {
        private object dataSource = null;

        private DataTable set = null;
        private bool dataSourceBound = false;

        public DataTable Set
        {
            get
            {
                return this.set ?? (this.set = new DataTable());
            }
            set
            {
                set = value;
            }
        }

        public object DataSource
        {
            set
            {
                this.dataSource = value;
                this.dataSourceBound = false;
            }
            private get
            {
                return this.dataSource;
            }
        }

        public override string ToXml()
        {
            if (this.dataSource != null && this.dataSourceBound == false)
                this.DataBind();

            FiChartXmlSerializer serializer = new FiChartXmlSerializer();
            return serializer.Serialize(this);
        }

        public string ToJson()
        {
            return JsonConvert.SerializeObject(this.dataSource);
        }

        public new void DataBind()
        {
            DataSourceParser parser = new DataSourceParser();

            this.Set = parser.Parse(this.dataSource);
            this.dataSourceBound = true;
        }

        [Bindable(true)]
        [DefaultValue("")]
        [Localizable(true)]
        public string JsonData
        {
            get
            {
                var s = (string)this.ViewState["JsonData"];
                return s ?? string.Empty;
            }

            set
            {
                this.ViewState["JsonData"] = value;
            }
        }

        /// <summary>
        /// 数据序列化类型
        /// </summary>
        public DataType DataType { get; set; }
    }
}
